import { useState } from 'react';
import axios from '../../components/service/request';

const ImageUpload = ({record , baseUir}) => {
  const [selectedImage, setSelectedImage] = useState(null);
  const handleImageChange = (e) => {
    setSelectedImage(e.target.files[0]);

  };

  const handleUpload = () => {
    const formData = new FormData();
    formData.append('image', selectedImage);
    formData.append('id', record.id);
    axios.post( baseUir + "/upload", formData)
      .then((res) => {
        console.log(res);
        alert("图片上传成功")
        // 处理上传成功的逻辑
      })
      .catch((err) => {
        console.error(err);
        alert("图片上传失败")
        // 处理上传失败的逻辑
      });
  };

  return (
    <div>
      <input type="file" onChange={handleImageChange} />
      <button onClick={handleUpload}>点击上传</button>
    </div>
  );
};

export default ImageUpload;